package com.wyu.notaryserver.exception;

import com.wyu.notaryserver.common.ResponseResult;
import com.wyu.notaryserver.enums.AppHttpCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     *process BizException
     * @param e
     * @return
     * 处理业务异常
     */
    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public ResponseResult bizExceptionHandler(BizException e){
        logger.error("biz exception！the reason is：{}",e.getErrorMsg());
        return ResponseResult.errorResult(Integer.parseInt(e.getErrorCode()),e.getErrorMsg());
    }
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResponseResult exceptionHandler(Exception e){
        logger.error(e.getMessage());
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }
}
